package tt.dz.egobus.servicemanager.dao.impl;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.stereotype.Repository;
import tt.dz.egobus.bean.BusinessStatus;
import tt.dz.egobus.servicemanager.base.dao.AbstractExtendDao;
import tt.dz.egobus.servicemanager.dao.BusinessServiceDao;
import tt.dz.egobus.servicemanager.entities.BusinessService;

import java.util.*;

/**
 * @author Anson
 * @date 17-3-16
 * @name 业务dao的实现类
 * @since 1.0.2
 */
@Repository("businessServiceDaoImpl")
public class BusinessServiceDaoImpl extends AbstractExtendDao<BusinessService, String> implements BusinessServiceDao {
    @Override
    protected void loadInstanceClass() {
        this.setType(BusinessService.class);
    }

    @Override
    public boolean isExitUserUnDoneBusiness(String userId) {
        Map<String, Object> params = new HashMap<String, Object>(1);
        params.put("userId", userId);
        int count = this.findCount("isExitUserUnDoneBusiness", params);
        return count > 0;
    }

    @Override
    public BusinessService getUserUnDoneService(String userId) {
        Map<String, Object> params = new HashMap<String, Object>(1);
        params.put("userId", userId);
        List<BusinessService> list = (List<BusinessService>) this.list("getUserUnDoneService", params, 0, 0, null);
        return list == null || list.isEmpty() ? null : list.get(0);
    }

    @Override
    public BusinessService getDriverUnDoneService(String driverId) {
        Map<String, Object> params = new HashMap<String, Object>(1);
        params.put("driverId", driverId);
        List<BusinessService> list = (List<BusinessService>) this.list("getDriverUnDoneService", params, 0, 0, null);
        return list == null || list.isEmpty() ? null : list.get(0);
    }

    @Override
    public List<Integer> queryDriverBusinessCodeCount(Integer[] businessCodes, String driverId) {
        if (businessCodes == null) {
            return null;
        }
        int length = businessCodes.length;
        Integer[] arrays = new Integer[length];
        List<Integer> list = new ArrayList<Integer>(Arrays.asList(arrays));
        Collections.fill(list, 0);
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(" select new map(count(t.businessCode) as count ,t.businessCode as businessCode ) from BusinessService t " +
                " where t.isValidate=true and t.driverId=:driverId and t.status=:status and t.businessCode in (:businessCodes) group by t.businessCode ");
        Session session = this.getSession();
        Query query = session.createQuery(stringBuilder.toString());
        query.setParameterList("businessCodes", businessCodes);
        query.setParameter("driverId", driverId);
        query.setParameter("status", BusinessStatus.FINISH.getCode());
        List<Map<?, ?>> mapList = query.list();
        if (mapList != null || !mapList.isEmpty()) {
            Map<Integer, Integer> converMap = new HashMap<Integer, Integer>();
            for (Map<?, ?> map : mapList) {
                converMap.put(Integer.valueOf(map.get("businessCode").toString()),
                        Integer.valueOf(map.get("count").toString()));
            }
            Integer businssCode = null;
            Integer count = null;
            for (int i = 0; i < length; i++) {
                businssCode = businessCodes[i];
                count = converMap.get(businssCode);
                count = count == null ? 0 : count;
                list.set(i, count);
            }
        }
        return list;
    }


}
